﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

#pragma warning disable IDE0130 // 命名空间与文件夹结构不匹配
namespace zykControl
#pragma warning restore IDE0130 // 命名空间与文件夹结构不匹配
{

    /// <summary>
    /// 用于编辑值的文本框
    /// </summary>
    /// <remarks>此文本框在回车时不发出响声</remarks>
    public class EditBase : TextBox
    {
        /// <summary>
        /// 用于编辑值的文本框
        /// </summary>
        public EditBase()
        {
            KeyDown += EditBox_KeyDown;
            KeyPress += EditBox_KeyPress;
            Enter += new EventHandler(EditBase_Enter);
            Leave += new EventHandler(EditBase_Leave);
            if (BackColorLeave.IsEmpty == false) BackColor = BackColorLeave;
        }

        private bool mEnterKeyTab = true;
        /// <summary>
        /// 当在文本框中回车时发生
        /// </summary>
        /// <remarks></remarks>
        [Description("当在文本框中回车时发生")]
        public event EventHandler? KeyEnter;

        /// <summary>
        /// 指示在此文本框中按Enter键是否向下移动焦点
        /// </summary>
        [Category("EnterKeyTab")]
        [Description("指示在此文本框中按Enter键是否向下移动焦点")]
        public bool EnterKeyTab
        {
            get
            {
                return mEnterKeyTab;
            }
            set
            {
                mEnterKeyTab = value;
            }
        }

        /// <summary>当按下回车键时被触发, 返回 True 表示把焦点移动到下一个控件, 返回 False 表示保持当前的焦点状态</summary>
        internal protected Func<bool>? OnKeyEnter { get; set; }

        /// <summary>控件得到焦点时的背景颜色</summary>
        [Description("控件得到焦点时的背景颜色")]
        public System.Drawing.Color BackColorEnter { get; set; } = System.Drawing.Color.Yellow;
        /// <summary>控件失去焦点后的背景颜色</summary>
        [Description("控件失去焦点后的背景颜色")]
        public System.Drawing.Color BackColorLeave { get; set; } = System.Drawing.Color.FromArgb(235, 255, 235);

        private void EditBox_KeyDown(object? sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
            KeyEnter?.Invoke(this, new EventArgs());
            if ((OnKeyEnter?.Invoke() ?? true) && mEnterKeyTab == true) SendKeys.Send("\t");
        }

        private void EditBox_KeyPress(object? sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r') e.Handled = true;
        }

        private void EditBase_Enter(object? sender, EventArgs e)
        {
            if (BackColorEnter.IsEmpty == false) BackColor = BackColorEnter;
        }

        private void EditBase_Leave(object? sender, EventArgs e)
        {
            if (BackColorLeave.IsEmpty == false) BackColor = BackColorLeave;
        }
    }

    /// <summary>
    /// 用于编辑文本值的文本框
    /// </summary>
    public class EditText : EditBase
    {
        /// <summary>
        /// 用于编辑文本值的文本框
        /// </summary>
        public EditText()
        {
            Enter += EditText_Enter;
        }

        private void EditText_Enter(object? sender, EventArgs e)
        {
            SelectAll();
        }
    }

    /// <summary>
    /// 用于编辑整数值的文本框
    /// </summary>
    public class EditInteger : EditBase
    {
        /// <summary>
        /// 用于编辑整数值的文本框
        /// </summary>
        public EditInteger()
        {
            Enter += EditBoxInteger_GotFocus;
            Leave += EditBoxInteger_LostFocus;
        }

        /// <summary>
        /// 返回/设置 整数值
        /// </summary>
        [Category("Value")]
        [Description("返回/设置 整数值")]
        public int Value
        {
            get
            {
                try
                {
                    _ = decimal.TryParse(Text, out var intValue);
                    return (int)intValue;
                }
                catch (Exception)
                {
                    return 0;
                }
            }

            set
            {
                Text = value.ToString();
            }
        }

        private void EditBoxInteger_GotFocus(object? sender, EventArgs e)
        {
            SelectAll();
        }

        private void EditBoxInteger_LostFocus(object? sender, EventArgs e)
        {
            try
            {
                _ = decimal.TryParse(Text, out var intValue);
                Text = ((int)intValue).ToString();
            }
            catch (Exception)
            {
                Text = "0";
            }
        }
    }

    /// <summary>
    /// 用于编辑金额值的文本框
    /// </summary>
    public class EditDecimal : EditBase
    {
        /// <summary>
        /// 用于编辑金额值的文本框
        /// </summary>
        public EditDecimal()
        {
            Enter += EditBoxDecimal_GotFocus;
            Leave += EditDecimal_LostFocus;
        }

        /// <summary>
        /// 返回/设置 金额值
        /// </summary>
        [Category("Value")]
        [Description("返回/设置 金额值")]
        public decimal Value
        {
            get
            {
                try
                {
                    _ = decimal.TryParse(Text, out var intValue);
                    return intValue;
                }
                catch (Exception)
                {
                    return 0m;
                }
            }

            set
            {
                if (value == 0)
                    Text = "";
                else
                    Text = value.ToString("0.00");
            }
        }

        private void EditBoxDecimal_GotFocus(object? sender, EventArgs e)
        {
            SelectAll();
        }

        private void EditDecimal_LostFocus(object? sender, EventArgs e)
        {
            try
            {
                _ = decimal.TryParse(Text, out var value);
                double dValue = (double)value;
                if (dValue == 0d)
                    Text = "";
                else
                    Text = dValue.ToString("0.00");
            }
            catch (Exception)
            {
                Text = "?";
            }
        }
    }

    /// <summary>
    /// 用于编辑小数值的文本框
    /// </summary>
    public class EditDouble : EditBase
    {
        /// <summary>
        /// 用于编辑小数值的文本框
        /// </summary>
        public EditDouble()
        {
            Enter += EditBoxDouble_GotFocus;
            Leave += EditBoxDouble_LostFocus;
        }

        private int mDigits = 2;

        /// <summary>
        /// 返回/设置 小数位数
        /// </summary>
        [Category("Digits")]
        [Description("返回/设置 小数位数")]
        public int Digits
        {
            get
            {
                return mDigits;
            }

            set
            {
                if (value > 6)
                    value = 6;
                if (value < 0)
                    value = 0;
                mDigits = value;
            }
        }

        /// <summary>
        /// 返回/设置 小数值
        /// </summary>
        [Category("Value")]
        [Description("返回/设置 小数值")]
        public double Value
        {
            get
            {
                try
                {
                    _ = decimal.TryParse(Text, out var value);
                    return (double)value;
                }
                catch (Exception)
                {
                    return 0d;
                }
            }

            set
            {
                value = Math.Round(value, mDigits);
                if (value == 0)
                    Text = "";
                else
                    Text = Math.Round(value, mDigits).ToString("g");
            }
        }

        private void EditBoxDouble_GotFocus(object? sender, EventArgs e)
        {
            SelectAll();
        }

        private void EditBoxDouble_LostFocus(object? sender, EventArgs e)
        {
            try
            {
                _ = decimal.TryParse(Text, out var value);
                double dValue = Math.Round((double)value, 2);
                if (dValue == 0d)
                    Text = "";
                else
                    Text = dValue.ToString("g");
            }
            catch (Exception)
            {
                Text = "?";
            }
        }
    }
}